package games.mapacman.common;

import games.mapacman.client.GameScreen;
import games.mapacman.server.MaPacmanZone;
import marauroa.common.game.RPClass;
import marauroa.common.game.RPObject;
import marauroa.common.game.Definition.Type;
import marauroa.server.game.Statistics;

public class Fruit extends Dot {

	private int fruitprob = 1;
	private int fruit_score;
	private boolean isFruit;

	public Fruit(int x, int y, int dot_score, int fruit_score,
			int fruit_probability, MaPacmanZone zone) {
		super(x, y, dot_score, zone);
		myObject.put("type", Constants.TYPE_FRUIT);
		myObject.put("fruitprob", fruit_probability);
		myObject.put("score", fruit_score);
		myObject.put("dottype", TYPE_FRUIT);
		fruitprob = fruit_probability;
		this.fruit_score = fruit_score;
		isFruit = true;
	}

	public Fruit(RPObject object) {
		super(object);
		isFruit = (object.getInt("dottype") == TYPE_FRUIT);

	}

	public static void initRPClass() {
		RPClass fruit = new RPClass(Constants.TYPE_FRUIT);
		fruit.isA(Constants.TYPE_DOT);
		fruit.addAttribute("dottype", Type.INT);
		fruit.addAttribute("fruitprob", Type.INT);
	}

	@Override
	public void setRespawnTime() {
		if (zone.getRandom().nextInt(fruitprob) == 0) { // Fruit
			myObject.put("score", fruit_score);
			myObject.put("dottype", TYPE_FRUIT);
			setRespawnTime(zone.getFruitRespawnTime());
		} else {
			myObject.put("dottype", TYPE_DOT);
			myObject.put("score", dot_score);
			setRespawnTime(zone.getDotRespawnTime());
		}
	}

	@Override
	public void addStats(Statistics stats) {
		if (myObject.getInt("dottype") == TYPE_FRUIT) {
			stats.add("Fruit eaten", 1);
		} else {
			super.addStats(stats);
		}
	}

	@Override
	public void draw(GameScreen screen) {
		if (isFruit) {
			screen.draw(screen.getDotSprite(TYPE_FRUIT), x, y);
		} else {
			screen.draw(screen.getDotSprite(TYPE_DOT), x, y);
		}
	}

}
